package com.egame.manage.biz.server.op;

import java.rmi.RemoteException;

import com.egame.manage.biz.server.ManageClientExt;

import cn.egame.common.data.BaseDao;
import cn.egame.common.exception.ExceptionCommonBase;
import cn.egame.ext.op.AdvDetail;
import cn.egame.ext.op.OperationActivityInfo;

public class StoreAdvertiseServiceDao extends BaseDao {
	public StoreAdvertiseServiceDao()throws ExceptionCommonBase {
		super("egame_ext");
	}
	
	public int deleteAdvertisement(int activityId)throws ExceptionCommonBase{
		String sql = "update t_operation_ad set status = '0' where id = " + activityId;
		return this.executeUpdate(sql);
	}
	
	public int upper(int appId, long loginUserId, int activityId) throws RemoteException {
		AdvDetail bean = ManageClientExt.getInstance().getAdvDetailById(appId, loginUserId, activityId);
		if(bean != null){
			AdvDetail upper = this.getUpperOperationAdvertiseInfo(appId, loginUserId, bean.getId(), bean.getRecordType(), bean.getPositionTypeId(), bean.getSortNo());
			if(upper != null){
				this.updateSortNo(appId, loginUserId, bean.getId(), upper.getSortNo());
				this.updateSortNo(appId, loginUserId, upper.getId(), bean.getSortNo());
				return 1;
			}
		}
		return -1;
	}
	
	public int lower(int appId, long loginUserId, int activityId) throws RemoteException {
		AdvDetail bean = ManageClientExt.getInstance().getAdvDetailById(appId, loginUserId, activityId);
		if(bean != null){
			AdvDetail lower = this.getLowerOperationAdvertiseInfo(appId, loginUserId, bean.getRecordType(), bean.getPositionTypeId(), bean.getSortNo());
			if(lower != null){
				this.updateSortNo(appId, loginUserId, bean.getId(), lower.getSortNo());
				this.updateSortNo(appId, loginUserId, lower.getId(), bean.getSortNo());
				return 1;
			}
		}
		return -1;
	}
	
	public AdvDetail getUpperOperationAdvertiseInfo(int appId, long loginUserId, int activityId,int type,int position,int sortNo)throws RemoteException{
		String sql = "select id from t_operation_ad" +
				" where record_type =  " + type +
				" and position_id =  " + position +
				" and sort_no >  " + sortNo +
				" ORDER BY sort_no asc LIMIT 1";
		int id = this.getInt(sql);
		System.out.println(sql);
		return ManageClientExt.getInstance().getAdvDetailById(appId, loginUserId, id);
	}
	
	public AdvDetail getLowerOperationAdvertiseInfo(int appId, long loginUserId, int type,int position,int sortNo)throws RemoteException{
		String sql = "select id from t_operation_ad" +
				" where record_type =  " + type +
				" and position_id =  " + position +
				" and sort_no <  " + sortNo +
				" ORDER BY sort_no desc LIMIT 1";
		int id = this.getInt(sql);
		return ManageClientExt.getInstance().getAdvDetailById(appId, loginUserId, id);
	}
	
	public int updateSortNo(int appId, long loginUserId, int activityId,int sortNo)throws ExceptionCommonBase{
		String sql = "update t_operation_ad set sort_no = " + sortNo + " where id = " + activityId;
		System.out.println(sql);
		return this.executeUpdate(sql);
	}
	
	public int getMaxSortNo(int type,int position)throws RemoteException{
		String sql = "select max(sort_no) from t_operation_ad" +
				" where record_type =  " + type +
				//" and position =  " + position +
				" and status = 1";
		return this.getInt(sql);
	}
}
